<template>
  <div>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/service/order'}">预约订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加预约订单</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <!-- 添加数据的表单 -->
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phoneNumber">
        <el-input v-model="ruleForm.phoneNumber"></el-input>
      </el-form-item>
      <el-form-item label="预约时间" prop="appointment">
        <el-input v-model="ruleForm.appointment" placeholder="例：2022/11/02 14:30:20"></el-input>
      </el-form-item>
      <el-form-item label="预约简介" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="医生姓名">
        <template>
          <el-select v-model="value" placeholder="选择医生">
            <el-option
                v-for="item in options"
                :key="item.name"
                :label="item.name"
                :value="item.id"
            >
            </el-option>
          </el-select>
        </template>


        <!--        <el-input v-model="ruleForm.doctorName"></el-input>-->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="warning" @click="gotoList">返回列表</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      label:"",

      value:"",
      options: [{
        value: '0',
        label: '过期订单'
      }, {
        value: '1',
        label: '正常预约'
      }, {
        value: '2',
        label: '进行中订单'
      }, {
        value: '3',
        label: '已完成订单'
      }],
      // 表单
      ruleForm: {
        username: '测试数据001',
        phoneNumber: "13556567878",
        appointment: "2022/12/12 12:12:30",
        description: '测试数据的简介001',
        doctorName: "",
        doctorId:""
      },
      // 表单验证规则
      rules: {
        name: [
          {required: true, message: '请输入预约订单名称', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入预约订单简介', trigger: 'blur'},
          {min: 2, max: 35, message: '长度在 4 到 35 个字符', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入排序序号', trigger: 'blur'},
          {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/, message: '排序序号必须是 0~99 之间的数字', trigger: 'blur'}
        ]
      }
    };
  },
  mounted() {
    this.ListDoctorName();
  },
  methods: {
    ListDoctorName() {
      let url = 'http://localhost:9999/order/listDoctorName';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.ruleForm);
      console.log('formData = ' + formData);

      let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
      this.axios
          .create({'headers': {'Authorization': loginInfo.token}})
          .get(url).then((response) => {

        let jsonResult = response.data;
        console.log("*******************************************************")
        console.log(jsonResult);
        if (jsonResult.state == 20000) {
          localStorage.setItem("key",JSON.stringify(jsonResult.data));

          this.options = jsonResult.data;
          console.log("*******************************************************")

          console.log(jsonResult.data);

        } else {

        }
      })
    },
    // 返回列表
    gotoList() {
      this.$router.push('./');
    },
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9999/order/add';
          console.log('url = ' + url);
          let addOrderDoctors = JSON.parse(localStorage.getItem("key"));
          console.log("addOrderDoctors",addOrderDoctors)

          for(let addOrderDoctor of addOrderDoctors){

            if (addOrderDoctor.id===this.value){
              console.log("addOrderDoctor",addOrderDoctor);
              this.ruleForm.doctorName = addOrderDoctor.name;
            }
          }
          this.ruleForm.doctorId = this.value;

          console.log("ruleForm",this.ruleForm)

          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
          this.axios
              .create({'headers': {'Authorization': loginInfo.token}})
              .post(url, formData).then((response) => {
            let jsonResult = response.data;
            console.log(jsonResult);
            if (jsonResult.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加预约订单成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: jsonResult.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
